package hfuu.com.cn.teasystemapp;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import hfuu.com.cn.teasystemapp.bean.land;
import hfuu.com.cn.teasystemapp.bean.tree;
import hfuu.com.cn.teasystemapp.util.HttpUtils;
import hfuu.com.cn.teasystemapp.util.SerializableMap;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by git on 2018/6/6.
 */

public class treeEditAcitivty extends AppCompatActivity {
    List<land> landList;
    Map m;
    private tree t;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_treeaddacivity);

        t=new tree();
        m=new HashMap();
        Intent o=getIntent();
        Bundle b=o.getExtras();
        SerializableMap serializableMap= (SerializableMap) b.get("map");
        m=serializableMap.getMap();
        landList = new ArrayList<land>();
        landList = (List<land>) o.getSerializableExtra("landlist");


        TextView textView= (TextView) findViewById(R.id.treetitle);
        textView.setText("采摘修改");
        ImageView back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Spinner sptype= (Spinner) findViewById(R.id.treestate);
        Spinner spland=(Spinner)findViewById(R.id.treeland);

        final EditText treevarity= (EditText) findViewById(R.id.treevarity);
        sptype.setDropDownVerticalOffset(30);
        spland.setDropDownVerticalOffset(30);

        treevarity.setText(m.get("treevarity").toString());
        t.setTreeId(m.get("treeid").toString().trim());

        List data=new ArrayList();

        for (land land:
                landList) {
            data.add(land.getLandName());
        }
        ArrayAdapter ad=new ArrayAdapter(this,R.layout.landitem,R.id.land,data);
        spland.setAdapter(ad);
        spland.setVisibility(View.VISIBLE);

        SpinnerAdapter sa = spland.getAdapter();
        for (int i = 0; i < sa.getCount(); i++) {
            String l= (String) sa.getItem(i);
            if (m.get("treelandname").toString().equals(l)) {
                spland.setSelection(i);
            }
        }
       // Log.i("state",m.get("treestate").toString());

        SpinnerAdapter sa1 = sptype.getAdapter();
        for (int i = 0; i < sa1.getCount(); i++) {
            String l= (String) sa1.getItem(i);
           // Log.i("treestate",m.get("treestate").toString().equals(l)+"");
            if (m.get("treestatename").toString().trim().equals(l)) {
                sptype.setSelection(i);
            }
        }

        Button okb= (Button) findViewById(R.id.ok);
        Button cab= (Button) findViewById(R.id.cancel);

        okb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String varity=treevarity.getText().toString().trim();
                if(varity.length()==0){
                    Toast.makeText(treeEditAcitivty.this,"请输入内容",Toast.LENGTH_SHORT).show();
                    return;
                }
                t.setTreeVarity(varity);
                Gson gson=new Gson();
                String json= gson.toJson(t);
                HttpUtils.doPost("tree/editprocad", json, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Looper.prepare();
                        Toast.makeText(treeEditAcitivty.this, "网络连接失败", Toast.LENGTH_LONG).show();
                        Looper.loop();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String res=response.body().string();
                        if(res.equals("success")){
                            Looper.prepare();
                            Toast.makeText(treeEditAcitivty.this, "修改成功", Toast.LENGTH_LONG).show();
                            Looper.loop();
                            return;
                        }else {
                            Looper.prepare();
                            Toast.makeText(treeEditAcitivty.this, "修改失败", Toast.LENGTH_LONG).show();
                            Looper.loop();
                            return;
                        }
                    }
                });
                finish();

            }
        });

        cab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                treeEditAcitivty.this.finish();
            }
        });


        /**
         * 监听下拉列表
         */
        sptype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String m = (String) parent.getSelectedItem();
                if(m.equals("正常")){
                    t.setTreeState(1);
                }else if(m.equals("异常")){
                    t.setTreeState(2);

                }else if(m.equals("坏死")){
                    t.setTreeState(0);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spland.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String m = (String) parent.getSelectedItem();
                for (land land:
                        landList) {
                    if(m.equals(land.getLandName()))
                        t.setTreeLand(land.getLandId());
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
